package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;

/**
 * @Name : IHomeServiceImpl.java
 * @Time : 2021/8/25 14:55
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */

@Service
@Slf4j
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    private PanelMapper panelMapper;

    @Autowired
    private ContentConverter contentConverter;

    @Autowired
    private PanelContentMapper panelContentMapper;

    @Override
    public HomePageResponse homepage() {

        HomePageResponse response = null;
        try {
//            List<Panel> panels = panelMapper.selectPanelContent();
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("position",0).andEqualTo("status",1);
            List<Panel> panels = panelMapper.selectByExample(example);
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                panel.setPanelContentItems(panelContentItems);
            }

            List<PanelDto> panelDtos = contentConverter.panel2DTO(panels);

            response = new HomePageResponse();
            response.setPanelContentItemDtos(new HashSet<>(panelDtos));
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IHomeServiceImpl.homepage occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }
}
